﻿using Gargoyle.Roton;
using System;
using System.Collections.Generic;
using System.Text;

namespace Gargoyle.ZZT
{
    public class Vector
    {
        Pointer pointer;

        public Vector()
        {
            pointer = new Pointer(new byte[4]);
        }

        public Vector(Memory memory)
        {
            pointer = memory.GetRelativePointer(0);
        }

        public Vector(Memory memory, int offset)
        {
            pointer = memory.GetRelativePointer(offset);
        }

        public Vector(Pointer pointer)
        {
            this.pointer = pointer;
        }

        public Vector(Int16 newX, Int16 newY)
        {
            pointer = new Pointer(new byte[4]);
            X = newX;
            Y = newY;
        }

        public static bool operator ==(Vector v1, Vector v2)
        {
            return ((v1.X == v2.X) && (v1.Y == v2.Y));
        }

        public static bool operator !=(Vector v1, Vector v2)
        {
            return ((v1.X != v2.X) || (v1.Y != v2.Y));
        }

        public Vector Add(Vector source)
        {
            return new Vector((Int16)(X + source.X), (Int16)(Y + source.Y));
        }

        public Vector Clockwise()
        {
            return new Vector((Int16)(-Y), X);
        }

        public Vector Clone()
        {
            return (Vector)MemberwiseClone();
        }

        public void CopyFrom(Vector source)
        {
            X = source.X;
            Y = source.Y;
        }

        public Vector CounterClockwise()
        {
            return new Vector(Y, (Int16)(-X));
        }

        public static Vector East()
        {
            return new Vector(1, 0);
        }

        public override bool Equals(object obj)
        {
            return base.Equals(obj);
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        public static Vector Idle()
        {
            return new Vector(0, 0);
        }

        public bool IsIdle
        {
            get
            {
                return ((X == 0) && (Y == 0));
            }
        }

        public static Vector North()
        {
            return new Vector(0, -1);
        }

        public Vector Opposite()
        {
            return new Vector((Int16)(-X), (Int16)(-Y));
        }

        public static Vector South()
        {
            return new Vector(0, 1);
        }

        public Vector Subtract(Vector source)
        {
            return new Vector((Int16)(X - source.X), (Int16)(Y - source.Y));
        }

        public Vector Swap()
        {
            return new Vector(Y, X);
        }

        public override string ToString()
        {
            return "Vector (" + X.ToString() + ", " + Y.ToString() + ")";
        }

        public static Vector West()
        {
            return new Vector(-1, 0);
        }

        public Int16 X
        {
            get { return pointer.GetInt16(0); }
            set { pointer.SetInt16(0, value); }
        }

        public Int16 Y
        {
            get { return pointer.GetInt16(2); }
            set { pointer.SetInt16(2, value); }
        }
    }
}
